package com.ui.contact;

import com.controller.ContactController;
import com.controller.GroupController;
import com.pojo.Contact;
import com.pojo.Group;
import com.ui.builder.ComboboxBuilder;
import com.ui.builder.FontBuilder;
import com.ui.builder.LabelBuilder;
import com.ui.builder.TextFieldBuilder;
import com.ui.utils.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class AddContact extends JFrame implements ActionListener {
    private final JPanel inputPanel = new JPanel();
    private final JPanel buttonPanel = new JPanel();
    private final JLabel nameLabel = LabelBuilder.makeLabel("姓名");
    private final JLabel phoneLabel = LabelBuilder.makeLabel("电话");
    private final JLabel genderLabel = LabelBuilder.makeLabel("性别");
    private final JLabel addressLabel = LabelBuilder.makeLabel("住址");
    private final JLabel emailLabel = LabelBuilder.makeLabel("邮箱");
    private final JLabel groupLabel = LabelBuilder.makeLabel("群组");
    private final JLabel starLabel = LabelBuilder.makeLabel("星标");
    private final JLabel[] labels = {nameLabel, phoneLabel, genderLabel, addressLabel, emailLabel, groupLabel, starLabel};
    private final JTextField nameInput = TextFieldBuilder.makeTextField(15);
    private final JTextField phoneInput = TextFieldBuilder.makeTextField(15);
    private final JComboBox<Object> genderChoose = ComboboxBuilder.makeCombobox("", "男", "女");
    private final JTextField addressInput = TextFieldBuilder.makeTextField(15);
    private final JTextField emailInput = TextFieldBuilder.makeTextField(15);
    private final JComboBox<Object> groupChoose = ComboboxBuilder.makeCombobox();
    private final JComboBox<Object> starChoose = ComboboxBuilder.makeCombobox("否", "是");
    private final JComponent[] inputs = {nameInput, phoneInput, genderChoose, addressInput, emailInput, groupChoose, starChoose};
    private final JButton submit = new JButton("确认");
    private final JButton reset = new JButton("重置");
    private final ContactController contactController = new ContactController();
    private final GroupController groupController = new GroupController();

    public AddContact() {
        super("添加联系人");

        //group choose
        List<Group> groupList = groupController.getAllGroups();
        if(groupList == null) {
            JOptionPane.showMessageDialog(null, "获取分组列表失败");
            return;
        }
        groupList.add(0, new Group(""));
        for(Group g: groupList) {
            groupChoose.addItem(g.getValue());
        }

        //button font and listener
        submit.setFont(FontBuilder.makeButtonFont());
        reset.setFont(FontBuilder.makeButtonFont());
        submit.addActionListener(this);
        reset.addActionListener(this);

        //inputPanel
        inputPanel.setLayout(new GridLayout(8, 2, 0, 10));
        for(int i = 0; i < labels.length; i++) {
            inputPanel.add(labels[i]);
            inputPanel.add(inputs[i]);
        }
        inputPanel.add(new JLabel());
        inputPanel.add(new JLabel());

        //button panel
        buttonPanel.setLayout(new GridLayout(1,2,30,0));
        buttonPanel.add(submit);
        buttonPanel.add(reset);

        //frame
        this.setLayout(new FlowLayout());
        this.setSize(400, 430);
        this.setLocationRelativeTo(null);
        this.add(inputPanel);
        this.add(buttonPanel);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if(source == submit) {
            Contact newContact = new Contact(
                    nameInput.getText(),
                    phoneInput.getText(),
                    (String) genderChoose.getSelectedItem(),
                    addressInput.getText(),
                    emailInput.getText(),
                    (String) groupChoose.getSelectedItem(),
                    starChoose.getSelectedItem() == "是"
            );
            int statusCode = new ContactFilter().checkParam(newContact);
            if(statusCode != 0) {
                Notification.sendNotification(StatusInformation.status[statusCode]);
                return;
            }
            List<Contact> queryList = contactController.findByPhoneAccurate(newContact.getPhone());
            if(queryList == null || !queryList.isEmpty()) {
                Notification.sendNotification(StatusInformation.status[6]);
                return;
            }
            boolean result = contactController.addPerson(newContact);
            if(result) {
                Notification.sendNotification(StatusInformation.status[0]);
                this.setVisible(false);
            }
            else {
                JOptionPane.showMessageDialog(null, "添加失败");
                this.setVisible(false);
            }
        }
        else if(source == reset) {
            for(JComponent input: inputs) {
                if(input instanceof JTextField) {
                    ((JTextField) input).setText("");
                }
                else if(input instanceof JComboBox<?>) {
                    ((JComboBox<?>) input).setSelectedIndex(0);
                }
            }
        }

    }
}
